#pragma once

#include <iostream>
#include "Protocal.hpp"

enum
{
    MOD_Zero = 1,
    Div_Zero,
    Other_Err
};


class ServerCal // calcult
{
    public:
    Respone CalculatorHelper(const Request &req)
    {
        Respone res(0, 0);
        switch (req.op_)
        {
        case '+':
            res.result_ = req.x_ + req.y_;
            break;
        case '-':
            res.result_ = req.x_ - req.y_;
            break;
        case '*':
            res.result_ = req.x_ * req.y_;
            break;
        case '/':
        {
            if (req.y_ == 0)
            {
                res.code_ = MOD_Zero;
            }
            else
                res.result_ = req.x_/ req.y_;
        }
        break;
        case '%':
        {
            if (req.y_ == 0)
            {
                res.code_ = Div_Zero;
            }
            else
                res.result_ = req.x_ % req.y_;
        }
        break;

        default:
            res.code_ = Other_Err;
            break;
        }

        return res;
    }


    string Calculator(string& package)  //"len"\n"x op y"\n
    {
        string content;
        bool r=Decode(package,&content); //去掉报头，提取字节流      x op y
        if(r==false) return "";//不动 package

        Request req;
        r=req.Deserialize(content);   //序列化
        if(!r) return "";

        Respone res=CalculatorHelper(req);//计算，序列化

        content="";
        res.Serialize(&content); //序列化 字节流
        
        content=Encode(content);//加包头
        return content;
    }
};